package com.xuhai

import java.io.{FileWriter, PrintWriter}

import com.xuhai.entity.GPSDATA
import com.xuhai.util.{MyJDBC, StringUtils}

/**
  * 收集最后每辆车的最后一个点的坐标
  */
object GatherGps {


  def main(args: Array[String]): Unit = {
    val citys = Array("520301", "522601","520100")

    val dateend = "20180831050000"
    val datestart = "20180830000000"

    val filewiter = new FileWriter("D:\\entryRate20180830.txt")
    val printWriter = new PrintWriter(filewiter)

    for(city <- citys){
      var count = 0
      val connect = MyJDBC.connect();
      val resultSet = MyJDBC.query("select busId, busNo from GJ_GPS_DATA_REALTIME where city = ?",city)
      while(resultSet.next()){
        count += 1
        val busId = resultSet.getString(1)
        val busNo = resultSet.getString(2)
        println("busNo  start: " + busNo)
        if(!StringUtils.isBlank(busId)){
          val subResultSet = MyJDBC.query("select city, busId, busNo, gpsDateTime, gpsLon, gpsLat from GJ_GPS_DATA where city = ? and busNo = ? and gpsDateTime > ? and gpsDateTime < ? order by gpsDateTime desc limit 1",city, busNo, datestart, dateend)
          while(subResultSet.next()){
            printWriter.println(subResultSet.getString(1)+ "," +subResultSet.getString(2)+ "," +subResultSet.getString(3)+ "," +subResultSet.getString(4)+ "," +subResultSet.getString(5)+ "," +subResultSet.getString(6))
          }
          subResultSet.close()
        }else{
          val subResultSet = MyJDBC.query("select city, busId, busNo, gpsDateTime, gpsLon, gpsLat from GJ_GPS_DATA where city = ? and busNo = ? and gpsDateTime > ? and gpsDateTime < ? order by gpsDateTime desc limit 1",city, busNo, datestart, dateend)
          while(subResultSet.next()){
            printWriter.println(subResultSet.getString(1)+ "," +subResultSet.getString(2)+ "," +subResultSet.getString(3)+ "," +subResultSet.getString(4)+ "," +subResultSet.getString(5)+ "," +subResultSet.getString(6))
          }
          subResultSet.close()
        }
        printWriter.flush()
        println("busNo  end: " + busNo)
      }
      connect.close()
      printWriter.println(city+","+count)
      printWriter.flush()
    }
    printWriter.close()


  }

}
